package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.dto.MenuDto;
import com.itheima.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.RoleMenuMapper;
import com.itheima.pojo.RoleMenu;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
@DubboService
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper,RoleMenu> implements RoleMenuService {

    @Override
    @Transactional
    public Boolean saveRoleMenuByRole(MenuDto menuDto) {
        Long roleId = menuDto.getRoleId();
        Long[] menuId = menuDto.getMenu();
        ArrayList<RoleMenu> roleMenuList = new ArrayList<>();
        for (Long mId : menuId) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(mId);
            roleMenu.setRoleId(roleId);
            roleMenuList.add(roleMenu);
        }
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        this.remove(queryWrapper);
        this.saveOrUpdateBatch(roleMenuList);
        return true;
    }

    @Override
    public Boolean deleteRoleUserByRoleId(Long id) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        return this.remove(queryWrapper);
    }
}